/**
 * 应用相关类型定义
 */
import { ThemeConfig, LocaleConfig, AppSettings } from './base.types'

/**
 * 应用信息
 */
export interface AppInfo {
  name: string
  version: string
  buildNumber?: string
  appPath: string
  locale: string
  isPackaged: boolean
  platform: NodeJS.Platform
  arch: string
  osVersion: string
  osName: string
  electronVersion: string
  chromeVersion: string
  nodeVersion: string
}

/**
 * 应用状态
 */
export interface AppState {
  isReady: boolean
  isQuitting: boolean
  hasUnsavedChanges: boolean
  currentProject?: string
  activeWindows: number
  lastActivity: string
}

/**
 * 窗口状态
 */
export interface WindowState {
  id: number
  isVisible: boolean
  isMinimized: boolean
  isMaximized: boolean
  isFullScreen: boolean
  isFocused: boolean
  bounds: {
    x: number
    y: number
    width: number
    height: number
  }
  title: string
  type: 'main' | 'splash' | 'settings' | 'about' | 'dialog'
}

/**
 * 窗口配置
 */
export interface WindowConfig {
  width?: number
  height?: number
  minWidth?: number
  minHeight?: number
  maxWidth?: number
  maxHeight?: number
  x?: number
  y?: number
  center?: boolean
  resizable?: boolean
  minimizable?: boolean
  maximizable?: boolean
  closable?: boolean
  alwaysOnTop?: boolean
  fullscreen?: boolean
  title?: string
  icon?: string
  show?: boolean
  frame?: boolean
  transparent?: boolean
  opacity?: number
}

/**
 * 菜单项配置
 */
export interface MenuItemConfig {
  id: string
  label: string
  type?: 'normal' | 'separator' | 'submenu' | 'checkbox' | 'radio'
  role?: string
  accelerator?: string
  icon?: string
  enabled?: boolean
  visible?: boolean
  checked?: boolean
  submenu?: MenuItemConfig[]
  click?: () => void
}

/**
 * 应用菜单配置
 */
export interface AppMenuConfig {
  template: MenuItemConfig[]
  autoHide?: boolean
  showInspectElement?: boolean
}

/**
 * 对话框选项
 */
export interface DialogOptions {
  type?: 'none' | 'info' | 'error' | 'question' | 'warning'
  title?: string
  message: string
  detail?: string
  buttons?: string[]
  defaultId?: number
  cancelId?: number
  noLink?: boolean
  normalizeAccessKeys?: boolean
}

/**
 * 文件对话框选项
 */
export interface FileDialogOptions {
  title?: string
  defaultPath?: string
  buttonLabel?: string
  filters?: Array<{
    name: string
    extensions: string[]
  }>
  properties?: Array<
    | 'openFile'
    | 'openDirectory'
    | 'multiSelections'
    | 'showHiddenFiles'
    | 'createDirectory'
    | 'promptToCreate'
    | 'noResolveAliases'
    | 'treatPackageAsDirectory'
    | 'dontAddToRecent'
  >
  message?: string
  securityScopedBookmarks?: boolean
}

/**
 * 保存对话框选项
 */
export interface SaveDialogOptions {
  title?: string
  defaultPath?: string
  buttonLabel?: string
  filters?: Array<{
    name: string
    extensions: string[]
  }>
  message?: string
  nameFieldLabel?: string
  showsTagField?: boolean
  properties?: Array<
    | 'showHiddenFiles'
    | 'createDirectory'
    | 'treatPackageAsDirectory'
    | 'showOverwriteConfirmation'
    | 'dontAddToRecent'
  >
  securityScopedBookmarks?: boolean
}

/**
 * 通知选项
 */
export interface NotificationOptions {
  title: string
  subtitle?: string
  body: string
  silent?: boolean
  icon?: string
  hasReply?: boolean
  timeoutType?: 'default' | 'never'
  replyPlaceholder?: string
  sound?: string
  urgency?: 'normal' | 'critical' | 'low'
  actions?: Array<{
    type: 'button'
    text: string
  }>
  closeButtonText?: string
  toastXml?: string
}

/**
 * 应用更新信息
 */
export interface UpdateInfo {
  version: string
  releaseDate: string
  releaseName?: string
  releaseNotes?: string
  downloadUrl: string
  size: number
  signature?: string
  isForced: boolean
  isPrerelease: boolean
}

/**
 * 更新检查结果
 */
export interface UpdateCheckResult {
  hasUpdate: boolean
  currentVersion: string
  latestVersion?: string
  updateInfo?: UpdateInfo
  error?: string
}

/**
 * 下载进度
 */
export interface DownloadProgress {
  percent: number
  bytesPerSecond: number
  total: number
  transferred: number
  eta?: number
}

/**
 * 应用性能指标
 */
export interface AppPerformance {
  memory: {
    used: number
    total: number
    external: number
    arrayBuffers: number
  }
  cpu: {
    percentCPUUsage: number
    idleWakeupsPerSecond: number
  }
  pid: number
  type: 'Browser' | 'Tab' | 'Utility' | 'Zygote' | 'Sandbox helper' | 'GPU' | 'Pepper Plugin' | 'Pepper Plugin Broker' | 'Unknown'
  creationTime: number
  sandboxed: boolean
  integrityLevel?: string
}

/**
 * 应用日志级别
 */
export type LogLevel = 'error' | 'warn' | 'info' | 'debug' | 'verbose'

/**
 * 应用日志条目
 */
export interface LogEntry {
  level: LogLevel
  message: string
  timestamp: string
  module?: string
  metadata?: any
  stack?: string
}

/**
 * 应用配置
 */
export interface AppConfig extends AppSettings {
  window: {
    main: WindowConfig
    splash: WindowConfig
    settings: WindowConfig
  }
  menu: AppMenuConfig
  updates: {
    enabled: boolean
    checkInterval: number
    autoDownload: boolean
    autoInstall: boolean
    allowPrerelease: boolean
  }
  logging: {
    level: LogLevel
    file: boolean
    console: boolean
    maxFiles: number
    maxSize: string
  }
  security: {
    allowRunningInsecureContent: boolean
    allowDisplayingInsecureContent: boolean
    experimentalFeatures: boolean
  }
  performance: {
    enableGPU: boolean
    enableWebSecurity: boolean
    enableNodeIntegration: boolean
    enableContextIsolation: boolean
  }
}

/**
 * 键盘快捷键
 */
export interface KeyboardShortcut {
  id: string
  name: string
  description: string
  accelerator: string
  global?: boolean
  enabled: boolean
  category: string
}

/**
 * 应用主题
 */
export interface AppTheme extends ThemeConfig {
  id: string
  name: string
  description?: string
  author?: string
  version?: string
  preview?: string
  colors: {
    primary: string
    secondary: string
    accent: string
    background: string
    surface: string
    text: string
    textSecondary: string
    border: string
    error: string
    warning: string
    success: string
    info: string
  }
  fonts: {
    primary: string
    secondary: string
    monospace: string
  }
  spacing: {
    xs: string
    sm: string
    md: string
    lg: string
    xl: string
  }
  borderRadius: {
    sm: string
    md: string
    lg: string
  }
  shadows: {
    sm: string
    md: string
    lg: string
  }
}

/**
 * 应用插件信息
 */
export interface PluginInfo {
  id: string
  name: string
  version: string
  description: string
  author: string
  homepage?: string
  repository?: string
  license: string
  keywords: string[]
  main: string
  enabled: boolean
  installed: boolean
  dependencies?: Record<string, string>
  permissions?: string[]
  config?: Record<string, any>
}

/**
 * 应用扩展点
 */
export interface ExtensionPoint {
  id: string
  name: string
  description: string
  type: 'menu' | 'toolbar' | 'panel' | 'dialog' | 'service'
  schema?: any
}

/**
 * 应用错误信息
 */
export interface AppError {
  id: string
  type: 'fatal' | 'error' | 'warning'
  message: string
  stack?: string
  timestamp: string
  module?: string
  version: string
  platform: string
  metadata?: any
  reported?: boolean
}
